<?php
/**
 * @copyright Copyright 2012 CrApache-GUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This module is for creation of SSL certificates using Opnessl
 * @author Jayanga Kaushalya
 */

/*
 * @var $certi_type string Certificate type
 * @var $exp_days string Expiration date
 * @var $digest string Key digesting algorithm
 * @var $encrypt string Encryption algorithm
 * @var $bitsize int Encryption algorith size
 * @var $certi_path string Pathe to store certificate
 * @var $key_path string Path to sotre key
 * @var $config string The configuration file location
 * @var $organization string The compnay name
 * @var $org_unit string Deaprtment name
 * @var $common_name string Full quallified domain name
 * @var $exe_string string Final execution string *
 */

function fill_data() {

    global $certi_type;
    global $exp_days;
    global $digest;
    global $encrypt;
    global $bitsize;
    global $certi_path;
    global $key_path;
    global $config_file;
    global $organization;
    global $org_unit;
    global $common_name;

    $certi_type = $_POST[];
    $exp_days = "-days ".$_POST[];
    $digest = $_POST[];
    $encrypt = $_POST[];
    $bitsize = $_POST[];
    $certi_path = $_POST[];
    $key_path = $_POST[];
    $config_file = "-config ".$_POST[];
    $organization = $_POST[];
    $org_unit = $_POST[];
    $common_name = $_POST[];

    global $exe_string;
    $exe_string = "openssl req -new $config_file "
                  ."$certi_type $exp_days $digest -newkey "
                  ."$encrypt:$bitsize -nodes -keyout $key_path -out $certi_path "
                  ." -subj \"/O=$organization/OU=$org_unit/CN=$common_name\"";
}

function generate(){

    global $exe_string;
    if(exec($exe_string, $output, $return) == 1)
            return false;
    else
        return true;
}

function run(){

    fill_data();

    if(generate())
        echo "ok";
    else
        echo "not-ok";
}

?>
